package com.supermarket.shop.system.dao.impl;

import com.supermarket.shop.system.dao.CommodityDao;
import com.supermarket.shop.system.domain.Commodity;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class CommodityDaoImpl implements CommodityDao {
    //向系统文件中写入commodity商品对象信息
    @Override
    public void BufferedWriterFile(Commodity com) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
        BufferedWriter bw = new BufferedWriter(new
                FileWriter("D:\\下载\\file\\超市商品购物系统\\commodity.txt", true));
        bw.write(com.getId() + ",");
        bw.write(com.getName() + ",");
        bw.write(com.getPrice() + ",");
        bw.write(com.getStock() + ",");
        bw.write(sdf.format(com.getDate())+ ",");
        bw.write(com.getPop());
        bw.newLine();
        bw.flush();
        bw.close();
    }

    //从系统文件中读取所有的商品信息
    @Override
    public ArrayList<Commodity> BufferedReaderFile() throws IOException {
        //创建接收商品信息的集合
        ArrayList<Commodity> list = new ArrayList<Commodity>();
        //创建文件
        File file=new File("D:\\下载\\file\\超市商品购物系统\\commodity.txt");
        file.createNewFile();
        //创建字符缓冲输入流
        BufferedReader br = new BufferedReader(new
                FileReader("D:\\下载\\file\\超市商品购物系统\\commodity.txt"));
        String[] split;
        String line = null;
        //循环遍历接收所有的商品信息
        while ((line = br.readLine()) != null) {
            split = line.split(",");
            //接收每一个商品信息的对象信息
            Commodity commodity= Commodity.getCommodity(split);
            list.add(commodity);
        }
        //关闭流
        br.close();
        //如果商品信息没有就返回空集合否则返回list集合
        if (list.size()==0) {
            return null;
        }
        return list;
    }

    //判断是否存在name的商品信息
    @Override
    public boolean isExist(String name) throws IOException {
        ArrayList<Commodity> list=this.BufferedReaderFile();
        if(list==null){
            return false;
        }
        for (Commodity commodity:list) {
            if(commodity!=null&&commodity.getName().equals(name)){
                return true;
            }
        }
        return false;
    }

    //找出所有的商品信息中id最大的值
    @Override
    public Integer maxCommodityId() throws IOException {
        ArrayList<Commodity> list=this.BufferedReaderFile();
        Integer max=0;
        if(list==null){
            return max;
        }
        for(Commodity commodity:list){
            if(commodity!=null&&max<commodity.getId()){
                max=commodity.getId();
            }
        }
        return max;
    }

    //增加一个商品信息
    @Override
    public boolean addCommodity(Commodity commodity) throws IOException {
        this.BufferedWriterFile(commodity);
        return true;
    }

    //查找所有的商品信息
    @Override
    public void findAllCommodity() throws IOException {
        ArrayList<Commodity> list=BufferedReaderFile();
        if(list==null){
            System.out.println("商品序号"+"\t\t\t"+"商品名称"+"\t\t\t"+"商品价格"+"\t\t\t"+"商品库存"+"\t\t\t"+"入库日期"+"\t\t\t"+"生产企业名称");
            System.out.println(" - - - - - - 目前还没有商品入库 - - - - - - ");
            return;
        }else{
            System.out.println("商品序号"+"\t\t\t"+"商品名称"+"\t\t\t"+"商品价格"+"\t\t\t"+"商品库存"+"\t\t\t"+"入库日期"+"\t\t\t"+"生产企业名称");
            for(Commodity commodity:list){
                commodity.show();
            }
        }
    }

    //通过查找到name商品信息,删除掉商品信息
    @Override
    public void deleteCommodityByName(String name) throws IOException {
        ArrayList<Commodity> list=BufferedReaderFile();
        if(list==null){
            System.out.println("你查找的文件里没有要找的信息!");
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i)!=null&&list.get(i).getName().equals(name)){
                list.remove(i);
                System.out.println("已经移除了!!");
                i--;
            }
        }
        this.deleteFile(list);
    }

    //通过传入name商品名称来重新设置商品信息,id保持不变
    @Override
    public void updateCommodityByname(Commodity commodity,String nameupdate) throws IOException {
        ArrayList<Commodity> list=BufferedReaderFile();
        if(list==null){
            System.out.println("你查找的文件里没有要找的信息!");
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i)!=null&&list.get(i).getName().equals(commodity.getName())){
                list.set(i,commodity);
                list.get(i).setName(nameupdate);
            }
        }
        deleteFile(list);

    }

    //删除文件,创建同名新文件,并上传list集合的商品信息
    public void deleteFile(ArrayList<Commodity> list) throws IOException {
        File file = new File("D:\\下载\\file\\超市商品购物系统\\commodity.txt");
        while (true) {
            if (file.exists() && file.isFile()) {
                file.delete();
            } else {
                break;
            }
        }
        for (Commodity commodity : list) {
            this.BufferedWriterFile(commodity);
        }
    }
}
